<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Pubfun extends FOX_Controller {

    function __construct ()
    {
        parent::__construct();
        $this->load->model('category_m');
        $this->load->model('good_m');
    }
    /**
     * 获取省市区联动
     */
    public function get_area() {
        $p_id = $this->input->get("p_id");

        $data = $this->areas_m->get_sons_by_pid($p_id);
        $html = '<option selected="selected" value="0">可选择</option>';
        if (!empty($data)) {
            foreach ($data as $obj) {
                $html .= '<option value="'.$obj['area_id'].'">'.$obj['area_name'].'</option>';
            }
        }
        echo $html;
    }

    /**
     * 获取经纬度新(因旧接口调取上海经纬度出bug)
     * @author fengchen@usuretech.com
     * @createtime 2016.11.7
     */
    public function get_location(){
        $this->load->library('curl');
        $get = $this->input->get();
        $address = $get["address"];
        $getData['address'] = $address;
        $getData['output'] = 'json';
        $getData['ak'] = '8526cdc9eaa7faa35706d8d0fe54528d';

        $get_url = 'http://api.map.baidu.com/geocoder/v2/?'.http_build_query($getData, NULL, '&');
        $returnStr = $this->curl->simple_get($get_url);
        $returnData = json_decode($returnStr, true);
        if($returnData["result"]["location"]["lat"]!=""){
            $msg = "经纬度获取成功 （地址类型:".$returnData["result"]["level"]."）";
            $arr["lat"]=$returnData["result"]["location"]["lat"];
            $arr["lng"]=$returnData["result"]["location"]["lng"];
            echo json_encode(array('status' => '1', 'msg' => $msg, 'arr' => $arr));
        }else{
            $msg = "经纬度获取失败，请确认你的输入后重新获取!";
            echo json_encode(array('status' => '2', 'msg' => $msg, 'arr' => $returnData));
        }
    }

    /**
     * 获取产品
     */
    public function get_good_category() {
        $cate_id = $this->input->get("cate_id");
        $user_id = $this->session->userdata("user_id");
        // 根据categoryid获取相应的产品
        $data = $this->good_m->get_goods_list_by_cate_id($cate_id,$user_id);
        if (!empty($data)) {
            $html = '<option selected="selected" value="0">可选择</option>';
            foreach ($data as $obj) {
                $html .= '<option value="'.$obj['good_id'].'">'.$obj['good_title'].' ( '.$obj['good_memo'].' )</option>';
            }
        }else{
            $html  = "0";
        }
        // 获取送货费
        $cateinfo=$this->category_m->get_category_by_id($cate_id);
        $category_tax = $cateinfo['category_tax']*100;
        echo json_encode(['html'=>$html,"cate"=>$category_tax]);exit();
    }


    public function get_good_property(){
        $product_id = $this->input->get("product_id");
        // 根据categoryid获取相应的产品
        $data = $this->good_m->get_good_property_by_id($product_id);
        if (!empty($data)) {
            $msg = "获取成功";
            echo json_encode(array('status' => '2', 'msg' => $msg, 'arr' => $data));
        }else{
            $msg = "获取失败";
            echo json_encode(array('status' => '2', 'msg' => $msg));
        }
    }

}
